package com.cqcvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqcvc.entity.Role;
import com.cqcvc.entity.UserRole;
import com.cqcvc.mapper.RoleMapper;
import com.cqcvc.mapper.UserRoleMapper;
import com.cqcvc.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hunter
 * @since 2022-08-04
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 保存用户角色信息
     * @param userRole
     * @return
     */
    @Override
    public int saveUserRole(UserRole userRole) {
        return userRoleMapper.insert(userRole);
    }

    /**
     * 删除用户角色信息
     * @param userRole
     * @return
     */
    @Override
    public int delUserRole(UserRole userRole) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",userRole.getUserId());
        qw.eq("role_id",userRole.getRoleId());
        return userRoleMapper.delete(qw);
    }

    @Override
    public Map<String,Object> searchByUid(Integer userId) {
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("user_id",userId);
        List<UserRole> list=userRoleMapper.selectList(qw);
        Map<String, Object> map=new HashMap<>();
        List<Integer> ids=new ArrayList<>();
        List<String> names=new ArrayList<>();
        for(UserRole ur:list){
            ids.add(ur.getRoleId());
            Role role=roleMapper.selectById(ur.getRoleId());
            names.add(role.getRoleName());
        }
        map.put("ids",ids);
        map.put("names",names);
        return map;
    }
}
